﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows;

namespace UI.ValueConverter
{
    internal class DateTimeTextHelper : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is DateTime)
            {
                var temp = (DateTime)value;
                return temp.ToString("yyyy-MM-dd HH:mm:ss");
            }
            else
            {
                return "未处理";
            }

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 没有标记时间时显示
    /// </summary>
    internal class HaveMarkTimeVisHelper : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is DateTime)
            {
                return Visibility.Visible;
            }
            else
            {
                return Visibility.Collapsed;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    /// <summary>
    /// 有标记时间时显示
    /// </summary>
    internal class NoMarkTimeVisHelper : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is DateTime)
            {
                return Visibility.Collapsed;
            }
            else
            {
                return Visibility.Visible;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }


    internal class DateTimePart2ValueConvertHelper : IValueConverter
    {
        DateTime dt;

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is DateTime)
            {
                dt = (DateTime)value;
                var type = parameter as string;

                string result = "00";
                switch (type)
                {
                    case "year":
                        result = $"{dt.Year}";
                        break;
                    case "month":
                        result = dt.Month < 10 ? $"0{dt.Month}" : $"{dt.Month}";
                        break;
                    case "day":
                        result = dt.Day < 10 ? $"0{dt.Day}" : $"{dt.Day}";
                        break;
                    case "hour":
                        result = dt.Hour < 10 ? $"0{dt.Hour}" : $"{dt.Hour}";
                        break;
                    case "minute":
                        result = dt.Minute < 10 ? $"0{dt.Minute}" : $"{dt.Minute}";
                        break;
                    case "second":
                        result = dt.Second < 10 ? $"0{dt.Second}" : $"{dt.Second}";
                        break;
                    default:
                        break;
                }

                return result;
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {

            if (value != null)
            {
                var type = parameter as string;
                try
                {
                    var changedValue = System.Convert.ToInt32(value);//变化的一个数字

                    var result = DateTime.Now;


                    switch (type)
                    {
                        case "year":
                            dt = new DateTime(changedValue, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second);
                            break;
                        case "month":
                            dt = new DateTime(dt.Year, changedValue, dt.Day, dt.Hour, dt.Minute, dt.Second);
                            break;
                        case "day":
                            dt = new DateTime(dt.Year, dt.Month, changedValue, dt.Hour, dt.Minute, dt.Second);
                            break;
                        case "hour":
                            dt = new DateTime(dt.Year, dt.Month, dt.Day, changedValue, dt.Minute, dt.Second);
                            break;
                        case "minute":
                            dt = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, changedValue, dt.Second);
                            break;
                        case "second":
                            dt = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, changedValue);
                            break;
                        default:
                            break;
                    }

                    return dt;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("时间格式不正确");
                    return dt;
                }
            }
            else
            {
                return DateTime.Now;
            }

        }
    }
}
